package org.jeecg.board.service.impl;/**
 * @Description
 * @Author LiCY
 * @Date 2023/4/24
 */

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.basic.entity.MesLineCfg;
import org.jeecg.basic.mapper.MesLineCfgMapper;
import org.jeecg.basic.service.IMesLineCfgService;
import org.jeecg.board.dto.*;
import org.jeecg.board.entity.RptStatisticansValue;
import org.jeecg.board.mapper.LineBoardMapper;
import org.jeecg.board.mapper.QisBoardMapper;
import org.jeecg.board.mapper.RptStatisticansValueMapper;
import org.jeecg.board.service.LineBoardService;
import org.jeecg.board.service.QisBoardService;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 功能描述：
 *
 * @author: LCY
 * @date: 2023年04月24日 14:01
 */
@Service
@Slf4j
public class QisBoardServiceImpl implements QisBoardService {
    @Autowired
    private LineBoardMapper lineBoardMapper;
    @Autowired
    private QisBoardMapper qisBoardMapper;
    @Autowired
    private RptStatisticansValueMapper rptStatisticansValueMapper;
    @Autowired
    private IMesLineCfgService iMesLineCfgService;
    @Autowired
    public RedisTemplate redisTemplate;


    @Override
    public Result<Map<String, Object>> getQisLineBoardData(Map<String, Object> param) {
        DynamicDataSourceContextHolder.clear();
        String line = "";
        if (param.containsKey("line")){
            line = (String) param.get("line");
        }
        DynamicDataSourceContextHolder.push("H"+line);
        HashMap<String, Object> map = new HashMap<>();
        String key = "qisLineBoardData"+"-"+line;
        log.info("查询产线质量看板数据开始{}", LocalDateTime.now());
        try {
            //查询当前班组、班次
            //1.获取今天的班组日期
            Integer dayStartTime = lineBoardMapper.getDayStartTime();
            //获取当前时间 判断当前时间如果小于开班时间则日期减一
            LocalDateTime now1 = LocalDateTime.now();
            LocalDateTime STime;
            int hour = now1.getHour();
            if (dayStartTime>hour){
                //日期减一
                STime = LocalDateTime.of(now1.minusDays(1).toLocalDate(), LocalTime.of(0, 0));
            }else{
                STime = LocalDateTime.of(now1.toLocalDate(), LocalTime.of(0, 0));
            }
                LocalDate shiftDate = STime.toLocalDate();
                param.put("shiftDate",shiftDate);
                //2.查询产线产量工序
                String firstOp = lineBoardMapper.getFirstOp(param);
                param.put("firstOp",firstOp);
                //3.查询产线成品工序
                String fgOp = lineBoardMapper.getFgOp(param);
                param.put("fgOp",fgOp);
                //统计当日产量数据
                ArrayList<RptStatisticansValue> list = new ArrayList<>();
                List<RptStatisticansValue> yieldList = qisBoardMapper.getLineYieldList(param);
                list.addAll(yieldList);
                //统计当日一次合格数据
                List<RptStatisticansValue> conformityList = qisBoardMapper.getLineConformityList(param);
                list.addAll(conformityList);
                //统计当日成品报废数据
                List<RptStatisticansValue> semiScrapList = qisBoardMapper.getLineSemiScrapList(param);
                list.addAll(semiScrapList);
                //统计当日半成品报废数据
                List<RptStatisticansValue> productScrapList = qisBoardMapper.getLineProductScrapList(param);
                list.addAll(productScrapList);
                if (!list.isEmpty()){
                    for (RptStatisticansValue rptStatisticansValue : list) {
                        System.out.println("Inserting data: " + rptStatisticansValue.toString());
                        rptStatisticansValueMapper.insertOrUpdate(rptStatisticansValue);
                    }
                }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DynamicDataSourceContextHolder.push("master");
            log.info("查询产线质量看板数据结束{}", LocalDateTime.now());
        }
        //1.获取当月、工单、昨日、班次-已包装、半成品报废、成品报废、待拍X光数量
        return null;
    }

    @Override
    public Result<Map<String, Object>> geQisLineBoardData(Map<String, Object> param) {
        Map<String, Object> map = new HashMap<>();
        //0 获取查询参数：当前班次日期、当前班次、前班次、当前工单号
        if (param.containsKey("shift")&&param.containsKey("woid")){
            String shift = param.get("shift").toString();
            String shiftId = param.get("shiftId").toString();
            int shiftSequence = Integer.parseInt(param.get("shiftSequence").toString());
            String woid = param.get("woid").toString();
            //1.获取今天的班组日期
            Integer dayStartTime = lineBoardMapper.getDayStartTime();
            //获取当前时间 判断当前时间如果小于开班时间则日期减一
            LocalDateTime now1 = LocalDateTime.now();
            LocalDateTime STime;
            int hour = now1.getHour();
            if (dayStartTime>hour){
                //日期减一
                STime = LocalDateTime.of(now1.minusDays(1).toLocalDate(), LocalTime.of(0, 0));
            }else{
                STime = LocalDateTime.of(now1.toLocalDate(), LocalTime.of(0, 0));
            }
            LocalDate curShiftDate = STime.toLocalDate();
            LocalDate preShiftDate;
            LocalDate preDate = curShiftDate.minusDays(1);
            if (shiftSequence-1==0){
                 preShiftDate = curShiftDate.minusDays(1);
            }else{
                 preShiftDate = curShiftDate;
            }
            //2.获取前班次名称
            String preShift = qisBoardMapper.getPreShiftName(shiftId,shift);
            //1.获取当月产品FTQ和Scrap
            List<QisFtqScrapBoardDataDTO>  ftqAndScrap  = qisBoardMapper.getMonthProductFtqAndScrap();
            ftqAndScrap.forEach(item->{
                if (item.getFtq()>100){
                    item.setFtq(100-item.getScrap());
                }
            });
            map.put("ftqAndScrap",ftqAndScrap);
            //2.获取当月过程检验趋势图
            List<QisInspectionBoardDataDTO>  inspectionPassRate  = qisBoardMapper.getMonthInspectionPassRate();
            map.put("inspectionPassRate",inspectionPassRate);
            //3.获取班次工站不良品统计
            //3.1前班次统计
            List<BoardDataDTO> preShiftOpUnhealthyList  = qisBoardMapper.getShiftOpUnhealthyList(curShiftDate,shift);
            map.put("preShiftOpUnhealthyList",preShiftOpUnhealthyList);
            //3.2当班次统计
            List<BoardDataDTO> curShiftOpUnhealthyList  = qisBoardMapper.getShiftOpUnhealthyList(preShiftDate,preShift);
            map.put("curShiftOpUnhealthyList",curShiftOpUnhealthyList);
            //4.获取最近7天产线FTQ&Scrap
            List<QisFtqScrapBoardDataDTO> ftqAndScrap7Days  = qisBoardMapper.getLastSevenDaysProductFtqAndScrap();
            map.put("ftqAndScrap7Days",ftqAndScrap7Days);
            //4.获取最近3天班次产线FTQ&Scrap
            List<QisFtqScrapBoardDataDTO> ftqAndScrap3Days  = qisBoardMapper.getLastThreeDaysShiftProductFtqAndScrap();
            map.put("ftqAndScrap3Days",ftqAndScrap3Days);
            //5.获取当月产线数据
            QisBoardDataDTO lineMonthData = qisBoardMapper.getLineQisData("month",woid,curShiftDate,shift);
            map.put("lineMonthData",lineMonthData);
            QisBoardDataDTO lineWoData = qisBoardMapper.getLineQisData("wo",woid,curShiftDate,shift);
            map.put("lineWoData",lineWoData);
            QisBoardDataDTO lineDayData = qisBoardMapper.getLineQisData("day",woid,curShiftDate,shift);
            map.put("lineDayData",lineDayData);
            QisBoardDataDTO lineShiftData = qisBoardMapper.getLineQisData("shift",woid,curShiftDate,shift);
            map.put("lineShiftData",lineShiftData);
            //获取产线不良top数据
            //10.当月产线FTQ&报废率
            List<UnhealthyBoardDataDTO> monthUnhealthyByLine = qisBoardMapper.getUnhealthyByLine("month",woid,curShiftDate,shift);
            //11.当工单产线FTQ&报废率
            List<UnhealthyBoardDataDTO> woUnhealthyByLine = qisBoardMapper.getUnhealthyByLine("wo",woid,curShiftDate,shift);
            //12.当日产线FTQ&报废率
            List<UnhealthyBoardDataDTO> dayUnhealthyByLine = qisBoardMapper.getUnhealthyByLine("day",woid,curShiftDate,shift);
            //13.当班次产线FTQ&报废率
            List<UnhealthyBoardDataDTO> shiftUnhealthyByLine = qisBoardMapper.getUnhealthyByLine("shift",woid,curShiftDate,shift);
            map.put("monthUnhealthyByLine",monthUnhealthyByLine);
            map.put("woUnhealthyByLine",woUnhealthyByLine);
            map.put("dayUnhealthyByLine",dayUnhealthyByLine);
            map.put("shiftUnhealthyByLine",shiftUnhealthyByLine);
            List<MesLineCfg> list = iMesLineCfgService.list();
            String name = list.get(0).getName();
            map.put("lineName",name);
            return Result.OK(map);
        }else {
            return Result.error("参数错误");
        }
    }
}
